package com.enjoyor.soa.traffic.util.base;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectUtil {

    public static Object getFieldValue(Object obj, String fieldName) {
        Object result = null;
        Field field = getField(obj, fieldName);
        if (field != null) {
            field.setAccessible(true);
            try {
                result = field.get(obj);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    private static Field getField(Object obj, String fieldName) {
        Field field = null;
        for (Class<?> clazz = obj.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
            try {
                field = clazz.getDeclaredField(fieldName);
            } catch (NoSuchFieldException localNoSuchFieldException) {
            }
        }
        return field;
    }

    public static void setFieldValue(Object obj, String fieldName, Object fieldValue) {
        Field field = getField(obj, fieldName);
        if (field != null) {
            try {
                field.setAccessible(true);
                if ((fieldValue instanceof String)) {
                    Object value = typeConversion(field.getType(), String.valueOf(fieldValue));
                    field.set(obj, value);
                } else {
                    field.set(obj, fieldValue);
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static Object invokeObjMethod(Object obj, String methodName, Class<?>[] paramTypes, Object[] params)
            throws Exception {
        Class<?> cls = obj.getClass();

        Method method = cls.getDeclaredMethod(methodName, paramTypes);

        method.setAccessible(true);
        Object val = method.invoke(obj, params);
        return val;
    }

    public static Object typeConversion(Class<?> clazz, String source) {
        if (clazz == null) {
            throw new IllegalArgumentException("clazz should not be null");
        }
        Object targetObj = null;
        String nameType = clazz.getName();
        if (("java.lang.Integer".equals(nameType)) || ("int".equals(nameType))) {
            targetObj = Integer.valueOf(source);
        } else if (("java.lang.String".equals(nameType)) || ("string".equals(nameType))) {
            targetObj = source;
        } else if (("java.lang.Float".equals(nameType)) || ("float".equals(nameType))) {
            targetObj = Float.valueOf(source);
        } else if (("java.lang.Double".equals(nameType)) || ("double".equals(nameType))) {
            targetObj = Double.valueOf(source);
        } else if (("java.lang.Boolean".equals(nameType)) || ("boolean".equals(nameType))) {
            targetObj = Boolean.valueOf(source);
        } else if (("java.lang.Long".equals(nameType)) || ("long".equals(nameType))) {
            targetObj = Long.valueOf(source);
        } else if (("java.lang.Short".equals(nameType)) || ("short".equals(nameType))) {
            targetObj = Short.valueOf(source);
        } else if (("java.lang.Character".equals(nameType)) || ("char".equals(nameType))) {
            targetObj = Character.valueOf(source.charAt(1));
        }
        return targetObj;
    }

}
